package com.quectel.cms.controller.patrol;


import com.quectel.cms.controller.BaseController;
import com.quectel.cms.vo.patrol.TaskCompleteReportVo;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.patrol.dto.VideoPatrolTaskDto;
import com.quectel.core.module.patrol.dto.VideoPatrolTaskSummaryDto;
import com.quectel.core.module.patrol.service.VideoPatrolTaskService;
import com.quectel.core.module.patrol.service.VideoPatrolTaskSummaryService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.JacksonUtils;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2020-07-07 15:48:10
 */
@RestController
@RequestMapping("videoPatrolTaskSummary")
@Tag(name = "VideoPatrolTaskSummaryController", description = "视频巡更任务统计相关api")
public class VideoPatrolTaskSummaryController extends BaseController {

    @DubboReference
    private VideoPatrolTaskSummaryService videoPatrolTaskSummaryService;
    @DubboReference
    private VideoPatrolTaskService videoPatrolTaskService;
    @DubboReference
    private VillageService villageService;

    /**
     * 视频巡更任务完成上报
     */
    @PostMapping("complete")
    @RequirePermissions(values = "videoPatrolTask:exec")//可以执行任务就可以上报
    @Operation(summary = "视频巡更任务完成上报")
    public Response<Object> complete(@RequestBody TaskCompleteReportVo vo) {
        VideoPatrolTaskDto videoPatrolTask = vo.getVideoPatrolTask();
        VideoPatrolTaskSummaryDto dbSummary = videoPatrolTaskSummaryService.queryByTaskId(videoPatrolTask.getId());
        if (dbSummary != null) {
            return Response.error("报告已生成无法重复提交");
        }

        //为了保险起见后端进行检验
        Date date = new Date();
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(date);
        String time = new SimpleDateFormat("HH:mm").format(date);
        VideoPatrolTaskDto dbTask = videoPatrolTaskService.selectById(videoPatrolTask.getId());
        if (!Objects.equals(dateStr, dbTask.getDate())
                || time.compareTo(dbTask.getPeriod().split("-")[0]) < 0
                || time.compareTo(dbTask.getPeriod().split("-")[1]) > 0
        ) {
            return Response.error("当前时间不在该任务的时间段范围之内");
        }

        VideoPatrolTaskSummaryDto videoPatrolTaskSummary = vo.getVideoPatrolTaskSummary();
        if (videoPatrolTaskSummary == null) {
            videoPatrolTaskSummary = new VideoPatrolTaskSummaryDto();
        }
        videoPatrolTaskSummary.setVillageId(videoPatrolTask.getVillageId());
        videoPatrolTaskSummary.setTaskId(videoPatrolTask.getId());
        videoPatrolTaskSummary.setTaskJson(JacksonUtils.toJsonString(videoPatrolTask));
        videoPatrolTaskSummary.setTaskName(videoPatrolTask.getName());
        videoPatrolTaskSummary.setSignDetail(JacksonUtils.toJsonString(vo.getSignInfoArr()));
        fillSaveCommonData(videoPatrolTaskSummary);
        videoPatrolTaskSummaryService.save(videoPatrolTaskSummary);
        //任务状态修改
        VideoPatrolTaskDto updateObj = new VideoPatrolTaskDto();
        updateObj.setId(dbTask.getId());
        updateObj.setExecStatus((byte) 1);
        videoPatrolTaskService.updateById(updateObj);
        return Response.ok();
    }

    /**
     * 视频巡更任务分页查询列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "videoPatrolTaskSummary:list")
    @Operation(summary = "视频巡更任务分页查询列表")
    public Response<Page<VideoPatrolTaskSummaryDto>> list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Request request = Request.configParams(params).like("execUsername");
        super.fillAuthorityData(params);
        int total = videoPatrolTaskSummaryService.queryTotal(params);

        List<VideoPatrolTaskSummaryDto> list = null;
        if (total > 0) {
            list = videoPatrolTaskSummaryService.queryList(params);
            for (VideoPatrolTaskSummaryDto videoPatrolTaskSummaryDto : list) {
                VillageDto villageDto = villageService.selectById(videoPatrolTaskSummaryDto.getVillageId());
                videoPatrolTaskSummaryDto.setVillageDto(villageDto);
            }
        } else {
            list = new ArrayList<>();
        }

        Page<VideoPatrolTaskSummaryDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<VideoPatrolTaskSummaryDto>>ok().wrap(page);
    }


    /**
     * 根据id查询视频巡更任务记录
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "videoPatrolTaskSummary:info")
    @Operation(summary = "根据id查询视频巡更任务记录")
    public Response<VideoPatrolTaskSummaryDto> info(@PathVariable("id") Long id) {

        VideoPatrolTaskSummaryDto videoPatrolTaskSummaryDto = videoPatrolTaskSummaryService.selectById(id);

        return Response.<VideoPatrolTaskSummaryDto>ok().wrap(videoPatrolTaskSummaryDto);
    }

    /**
     * 保存视频巡更任务记录
     */
    @PostMapping("save")
    @RequirePermissions(values = "videoPatrolTaskSummary:edit")
    @Operation(summary = "保存视频巡更任务记录")
    public Response<Object> save(@RequestBody @Validated VideoPatrolTaskSummaryDto videoPatrolTaskSummaryDto) {

        //填充基础数据
        fillSaveCommonData(videoPatrolTaskSummaryDto);

        videoPatrolTaskSummaryService.save(videoPatrolTaskSummaryDto);

        return Response.ok();
    }

    /**
     * 修改视频巡更任务记录
     */
    @PostMapping("update")
    @RequirePermissions(values = "videoPatrolTaskSummary:edit")
    @Operation(summary = "修改视频巡更任务记录")
    public Response<Object> update(@RequestBody @Validated VideoPatrolTaskSummaryDto videoPatrolTaskSummaryDto) {

        //填充基础数据
        fillEditCommonData(videoPatrolTaskSummaryDto);

        videoPatrolTaskSummaryService.updateById(videoPatrolTaskSummaryDto);

        return Response.ok();
    }

    /**
     * 批量删除视频巡更任务记录
     */
    @PostMapping("delete")
    @RequirePermissions(values = "videoPatrolTaskSummary:delete")
    @Operation(summary = "批量删除视频巡更任务记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        videoPatrolTaskSummaryService.deleteBatch(ids);

        return Response.ok();
    }
}
